//
//  ManagerData.m
//  NhomThuy
//
//  Created by HOCVIEN on 1/27/15.
//  Copyright (c) 2015 t3h. All rights reserved.
//

#import "ManagerData.h"
static ManagerData* instance;
@implementation ManagerData
+ (ManagerData*)defaultManager{
    if(!instance){
        instance = [[ManagerData alloc]init];
        instance.vnSongList = [[NSMutableArray alloc]init];
        instance.enSongList = [[NSMutableArray alloc]init];
        instance.singerList = [[NSMutableArray alloc]init];
        instance.loveSongList = [[NSMutableArray alloc]init];
        [instance loadData_v2];
    }
    return instance;
}
- (void)loadData_v2{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"KaraokeVietnam2" ofType:@"sqlite"];
    if (sqlite3_open([path UTF8String], &_database)== SQLITE_OK) {
        //English Song
        char *sql = "Select ZSLYRIC, ZSMETA, ZROWID,  ZSNAME from ZSONG Where ZSLANGUAGE = 'en'";
        sqlite3_stmt *statement;
        if(sqlite3_prepare_v2(self.database, sql, -1, &statement, nil)== SQLITE_OK){
            while(sqlite3_step(statement)==SQLITE_ROW ){
                NSMutableDictionary *song_e = [[NSMutableDictionary alloc]init];
                [song_e setObject:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)] forKey:@"lyrics"];
                [song_e setObject:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)] forKey:@"singer-id"];
                [song_e setObject:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 2)] forKey:@"song-code"];
                [song_e setObject:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 3)] forKey:@"song-name"];
                [self.enSongList addObject:song_e];
                
            }
            sqlite3_finalize(statement);
        }
        //Viet Nam Song
        sql = "Select ZSLYRIC, ZSMETA, ZROWID,  ZSNAME from ZSONG Where ZSLANGUAGE = 'vn'";
        if(sqlite3_prepare_v2(self.database, sql, -1, &statement, nil)== SQLITE_OK){
            while(sqlite3_step(statement)==SQLITE_ROW ){
                NSMutableDictionary *song_v = [[NSMutableDictionary alloc]init];
                [song_v setObject:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)] forKey:@"lyrics"];
                [song_v setObject:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)] forKey:@"singer-id"];
                [song_v setObject:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 2)] forKey:@"song-code"];
                [song_v setObject:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 3)] forKey:@"song-name"];
                [self.vnSongList addObject:song_v];
                
            }
            sqlite3_finalize(statement);
        }
        sqlite3_close(self.database);
    }
    NSDictionary *file;
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"lovesong.plist"];
    BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:docPath];
    if(!exists){
        path = [[NSBundle mainBundle]pathForResource:@"lovesong" ofType:@"plist"];
        file = [NSDictionary dictionaryWithContentsOfFile:path];
        [file writeToFile:docPath atomically:YES];
    }else{
        file = [NSDictionary dictionaryWithContentsOfFile:docPath];
    }
    self.loveSongList = [[file objectForKey:@"love-songs"] mutableCopy];
}
-(void)loadData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
    NSDictionary *file = [NSDictionary dictionaryWithContentsOfFile:path];
    self.vnSongList = [file objectForKey:@"vi-songs"];
    self.enSongList = [file objectForKey:@"en-songs"];
    self.singerList = [file objectForKey:@"singers"];
    
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"lovesong.plist"];
    BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:docPath];
    if(!exists){
        path = [[NSBundle mainBundle]pathForResource:@"lovesong" ofType:@"plist"];
        file = [NSDictionary dictionaryWithContentsOfFile:path];
        [file writeToFile:docPath atomically:YES];
    }else{
        file = [NSDictionary dictionaryWithContentsOfFile:docPath];
    }
    self.loveSongList = [[file objectForKey:@"love-songs"] mutableCopy];
}
- (void)addLoveSongWithDictionaryOfSong:(NSDictionary *)song{
    if(self.loveSongList == nil)
    {
        self.loveSongList = [[NSMutableArray alloc]init];
    }
    [self.loveSongList addObject:song];
    NSDictionary *file = [[NSDictionary alloc]initWithObjectsAndKeys:self.loveSongList,@"love-songs", nil];
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"lovesong.plist"];
    NSLog(@"%@",docPath);
    [file writeToFile:docPath atomically:YES];
}
- (void)removeLoveSongWithDictionaryOfSong:(NSDictionary *)song{
    if(self.loveSongList == nil)
    {
        self.loveSongList = [[NSMutableArray alloc]init];
    }
    [self.loveSongList removeObject:song];
    NSDictionary *file = [[NSDictionary alloc]initWithObjectsAndKeys:self.loveSongList,@"love-songs", nil];
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"lovesong.plist"];
    [file writeToFile:docPath atomically:YES];
}
- (NSMutableArray *)getSongListBySingerID:(NSString*)singerID{
    NSMutableArray *array = [[NSMutableArray alloc]init];
    for (NSDictionary *song in self.vnSongList) {
        if ([[song objectForKey:@"singer-id"] isEqualToString:singerID]) {
            [array addObject:song];
        }
    }
    for (NSDictionary *song in self.enSongList) {
        if ([[song objectForKey:@"singer-id"] isEqualToString:singerID]) {
            [array addObject:song];
        }
    }
    return array;
}

- (NSString *)getSingerNameBySingerID:(NSString *)singerID
{

    for (NSDictionary *singer in self.singerList) {
        if ([[singer objectForKey:@"singer-id"] isEqualToString:singerID]) {
            return [singer objectForKey:@"singer-name"];
        }
    }
    return @"N/A";
}

- (BOOL)isAlreadyLikeSong:(NSString*)songID
{
    for (NSDictionary *loveSong in self.loveSongList) {
        if ([[loveSong objectForKey:@"song-code"] isEqualToString:songID]) {
            return YES;
        }
    }
    return NO;
}

- (NSMutableArray *) getLoveSongList
{
    return self.loveSongList;
}
@end
